<?php

namespace Model\Room;

use Extension\Tools;
use Model\User;
use Handler\ZoneHandler;
use Model\RoomBase;

/**
 * 算法
 *
 * @author AlexLin
 *        
 */
class Algorithm {
	/**
	 *
	 * @var RoomMain
	 */
	private $m_RoomMain;
	/**
	 * 扑克牌
	 *
	 * @var string[]
	 */
	private $m_Pokers;
	/**
	 * 玩家
	 *
	 * @var Player[]
	 */
	private $m_Players;
	/**
	 * 牛牛倍数
	 *
	 * @var int[]
	 */
	private $m_NiuNiuMul;
	/**
	 * 底分
	 *
	 * @var unknown
	 */
	private $m_DiFen;
	/**
	 *
	 * @var unknown
	 */
	private $m_MinEnter;
	/**
	 *
	 * @var unknown
	 */
	private $m_MaxEnter;
	/**
	 * 抽水值
	 *
	 * @var float
	 */
	private $m_ChouShui;
	/**
	 * 难度
	 *
	 * @var int
	 */
	private $m_Difficulty;
	/**
	 * 最大人数
	 */
	public const MAXUSER = 10;
	/**
	 * 构造函数
	 */
	function __construct($roomMain, $diFen, $minEnter, $maxEnter, $chouShui, $difficulty) {
		$this->m_RoomMain = $roomMain;
		
		$this->m_DiFen = $diFen;
		$this->m_MinEnter = $minEnter;
		$this->m_MaxEnter = $maxEnter;
		$this->m_ChouShui = $chouShui;
		$this->m_Difficulty = $difficulty;
		
		$this->m_Players = array ();
		for($i = 0; $i < self::MAXUSER; $i ++) {
			$this->m_Players [] = null;
		}
		// $this->m_Players = array (
		// null,
		// null,
		// null,
		// null,
		// null
		// );
		$this->m_NiuNiuMul = array (
				1,
				1,
				1,
				1,
				1,
				1,
				1,
				2,
				2,
				3,
				4,
				5,
				5,
				6,
				7,
				8,
				10,
				10 
		);
	}
	/**
	 * 析构函数
	 */
	function __destruct() {
	}
	/**
	 * 初始化一副扑克牌
	 */
	private function mb_InitPokers() {
		$this->m_Pokers = array ();
		for($i = 0; $i < 4; $i ++) {
			$color = 'A';
			if ($i == 1)
				$color = 'B';
			else if ($i == 2)
				$color = 'C';
			else if ($i == 3)
				$color = 'D';
			
			for($j = 1; $j < 14; $j ++) {
				$this->m_Pokers [] = $j . $color;
			}
		}
	}
	/**
	 * 重置开始
	 *
	 * @param bool $isInit
	 *        	是否一开始初始化
	 */
	public function mb_ReStart() {
		for($i = 0; $i < count ( $this->m_Players ); $i ++) {
			
			if (isset ( $this->m_Players [$i] )) {
				$isOnline = $this->m_Players [$i]->m_IsOnline;
				if ($isOnline == false) {
					ZoneHandler::mb_GetInstance ()->mb_ExitRoom ( $this->m_RoomMain->m_RoomId, $this->m_Players [$i]->m_User );
				}
			}
		}
		
		for($i = 0; $i < count ( $this->m_Players ); $i ++) {
			if (isset ( $this->m_Players [$i] ) && ($this->m_Players [$i]->m_User->m_Coin < $this->m_MinEnter || $this->m_Players [$i]->m_User->m_Coin > $this->m_MaxEnter)) {
				ZoneHandler::mb_GetInstance ()->mb_ExitRoom ( $this->m_RoomMain->m_RoomId, $this->m_Players [$i]->m_User );
			}
		}
		
		if ($this->m_RoomMain->m_RoomType == RoomBase::TYPE_BUDDY && $this->m_RoomMain->m_Round != 1) {
			if ($this->mb_GetPlayerCount () == 0)
				ZoneHandler::mb_GetInstance ()->mb_DestoryRoom ( $this->m_RoomMain->m_RoomId );
		}
		
		$this->mb_InitPokers ();
		for($i = 0; $i < count ( $this->m_Players ); $i ++) {
			if (isset ( $this->m_Players [$i] ))
				$this->m_Players [$i]->mb_ReStart ();
		}
	}
	/**
	 * 加入玩家
	 *
	 * @param User $user        	
	 * @return boolean
	 */
	public function mb_AddPlayer($user) {
		if ($user->m_Coin < $this->m_MinEnter || $user->m_Coin > $this->m_MaxEnter) {
			Tools::mb_LogResult ( "$user->m_Coin" );
			Tools::mb_LogResult ( "$user->m_Name 金币数超出房间限制" );
			return false;
		}
		
		$roomName = $this->m_RoomMain->m_RoomName;
		
		Tools::mb_LogResult ( "$user->m_Name 申请进入 $roomName !" );
		$player = $this->mb_GetPlayerForName ( $user->m_Name );
		if (isset ( $player )) {
			Tools::mb_LogResult ( "$user->m_Name 已在 $roomName , 重连玩家" );
			$player->mb_ReConnect ( $user );
		} else {
			if ($this->mb_GetPlayerCount () >= self::MAXUSER) {
				Tools::mb_LogResult ( "$roomName 已满人" );
				return false;
			}
			for($i = 0; $i < self::MAXUSER; $i ++) {
				if (isset ( $this->m_Players [$i] ) == false) {
					Tools::mb_LogResult ( "$user->m_Name 进入  $roomName ! 在 $i 号座坐下" );
					$this->m_Players [$i] = new Player ( $i, $user, $this->m_RoomMain );
					$this->m_RoomMain->mb_PushPlayerIn ( $this->m_Players [$i] );
					break;
				}
			}
		}
		return true;
	}
	/**
	 * 移除玩家
	 *
	 * @param User $user        	
	 * @return boolean
	 */
	public function mb_RemovePlayer($user) {
		$roomName = $this->m_RoomMain->m_RoomName;
		$bResult = true;
		if ($this->m_RoomMain->m_CurStatus == RoomMain::STATUS_WAIT) {
			for($i = 0; $i < count ( $this->m_Players ); $i ++) {
				if (isset ( $this->m_Players [$i] ) && $this->m_Players [$i]->m_User->m_Name == $user->m_Name) {
					$this->m_RoomMain->mb_PushPlayerOut ( $i );
					$this->m_RoomMain->mb_PushPlayerExit ( $this->m_Players [$i]->m_User );
					Tools::mb_LogResult ( "$user->m_Name 退出  $roomName ! " );
					$bResult = true;
					$this->m_Players [$i] = null;
					break;
				}
			}
			
			if ($this->m_RoomMain->m_RoomType == RoomBase::TYPE_BUDDY) {
				if ($this->mb_GetPlayerCount () == 0)
					ZoneHandler::mb_GetInstance ()->mb_DestoryRoom ( $this->m_RoomMain->m_RoomId );
			}
		} else {
			Tools::mb_LogResult ( "$user->m_Name 强制退出  $roomName ! " );
			
			$player = $this->mb_GetPlayerForName ( $user->m_Name );
			if ($player->m_User->m_IsRobot) {
				$player->mb_DisConnect ();
				$bResult = false;
			} else {
				if ($player->m_IsPlaying) {
					$player->mb_DisConnect ();
					$bResult = false;
				} else {
					for($i = 0; $i < count ( $this->m_Players ); $i ++) {
						if (isset ( $this->m_Players [$i] ) && $this->m_Players [$i]->m_User->m_Name == $user->m_Name) {
							$this->m_RoomMain->mb_PushPlayerOut ( $i );
							$this->m_RoomMain->mb_PushPlayerExit ( $this->m_Players [$i]->m_User );
							Tools::mb_LogResult ( "$user->m_Name 退出  $roomName ! " );
							$bResult = true;
							$this->m_Players [$i] = null;
							break;
						}
					}
				}
			}
		}
		return $bResult;
	}
	// ----------------------------------逻辑------------------------------------------//
	/**
	 * 发牌
	 */
	public function mb_SendPokers() {
		for($i = 0; $i < count ( $this->m_Players ); $i ++) {
			$player = $this->m_Players [$i];
			if (isset ( $player ) == false || $player->m_IsReady == false)
				continue;
			
			$player->m_IsPlaying = true;
			// if ($player->m_User->m_Name == '1') {
			// $player->m_Pokers = $this->mb_GetFivePokers ();
			
			// // 同花顺
			// $player->m_Pokers = array (
			// '3A',
			// '5A',
			// '6A',
			// '4A',
			// '2A'
			// );
			// return;
			// // 五小牛
			// $player->m_Pokers = array (
			// '1A',
			// '1B',
			// '1A',
			// '2B',
			// '2A'
			// );
			// // 炸弹牛
			// $player->m_Pokers = array (
			// '11A',
			// '11B',
			// '11C',
			// '11D',
			// '3A'
			// );
			// // 葫芦牛
			// $player->m_Pokers = array (
			// '10A',
			// '10B',
			// '2A',
			// '10C',
			// '2B'
			// );
			// // 同花牛
			// $player->m_Pokers = array (
			// '1A',
			// '7A',
			// '2A',
			// '5A',
			// '3A'
			// );
			// // 五花牛
			// $player->m_Pokers = array (
			// '10A',
			// '10B',
			// '11A',
			// '11B',
			// '12A'
			// );
			// // 顺子牛
			// $player->m_Pokers = array (
			// '5A',
			// '6B',
			// '7A',
			// '8B',
			// '9A'
			// );
			// } else {
			// $player->m_Pokers = $this->mb_GetFivePokers ();
			// // $player->m_Pokers = array (
			// // '3A',
			// // '5A',
			// // '6A',
			// // '4A',
			// // '2A'
			// // );
			// }
			if(count($player->m_PresetPokers) == 0){
				$player->m_Pokers = $this->mb_GetFivePokers ();
			}else {
				$player->m_Pokers = $player->m_PresetPokers;
			}
			
		}
	}
	/**
	 * 抢庄
	 *
	 * @param User $user        	
	 * @param int $bankerMul        	
	 */
	public function mb_QiangZhuang($user, $bankerMul) {
		$player = $this->mb_GetPlayerForName ( $user->m_Name );
		$player->mb_QiangZhuang ( $bankerMul );
		
		$this->m_RoomMain->mb_PushPlayerQiangZhuang ( $user->m_Name, $bankerMul );
		
		if ($this->mb_CheckAllPlayersIsQiangZhuang ()) {
			// 所有人都抢庄了，开始下注
			$this->m_RoomMain->mb_EnterDownBet ();
		}
	}
	/**
	 * 检查是否所有人都已抢庄
	 *
	 * @return boolean
	 */
	public function mb_CheckAllPlayersIsQiangZhuang() {
		for($i = 0; $i < count ( $this->m_Players ); $i ++) {
			if (isset ( $this->m_Players [$i] ) && $this->m_Players [$i]->m_IsPlaying && $this->m_Players [$i]->m_IsQiangZhuang == false) {
				// 还有人没抢庄
				return false;
			}
		}
		return true;
	}
	/**
	 * 所有玩家已抢庄，开始确认庄家，并返回要随机的是哪几个账号
	 */
	public function mb_ConfirmBanker() {
		$mul = 0;
		for($i = 0; $i < count ( $this->m_Players ); $i ++) {
			if (isset ( $this->m_Players [$i] ) && $this->m_Players [$i]->m_IsPlaying && $this->m_Players [$i]->m_BankerMul > $mul)
				$mul = $this->m_Players [$i]->m_BankerMul;
		}
		
		$bankers = array ();
		for($i = 0; $i < count ( $this->m_Players ); $i ++) {
			if (isset ( $this->m_Players [$i] ) && $this->m_Players [$i]->m_IsPlaying && $this->m_Players [$i]->m_BankerMul == $mul)
				$bankers [] = $this->m_Players [$i]->m_User->m_Name;
		}
		$index = mt_rand ( 0, count ( $bankers ) - 1 );
		$player = $this->mb_GetPlayerForName ( $bankers [$index] );
		$player->m_IsBanker = true;
		
		if ($mul == 0)
			$player->m_BankerMul = 1;
		
		return $bankers;
	}
	/**
	 * 闲家投注
	 *
	 * @param unknown $user        	
	 * @param unknown $bet        	
	 */
	public function mb_DownBet($user, $bet) {
		$player = $this->mb_GetPlayerForName ( $user->m_Name );
		$player->m_IsBet = true;
		$player->m_BetMul = $bet;
		$this->m_RoomMain->mb_PushPlayerDownBet ( $user->m_Name, $bet );
		
		if ($this->mb_CheckAllPlayersIsBet ()) {
			// 所有人都下注了，开始翻牌
			$this->m_RoomMain->mb_EnterOpen ();
		}
	}
	/**
	 * 检查是否所有玩家已下注
	 *
	 * @return boolean
	 */
	public function mb_CheckAllPlayersIsBet() {
		for($i = 0; $i < count ( $this->m_Players ); $i ++) {
			if (isset ( $this->m_Players [$i] ) && $this->m_Players [$i]->m_IsPlaying && $this->m_Players [$i]->m_IsBanker == false && $this->m_Players [$i]->m_IsBet == false) {
				// 还有人没下注
				return false;
			}
		}
		return true;
	}
	/**
	 * 玩家开牌
	 *
	 * @param User $user        	
	 */
	public function mb_OpenPoker($user) {
		$player = $this->mb_GetPlayerForName ( $user->m_Name );
		$player->m_IsOpen = true;
		$player->m_Pokers = $player->m_CleanUpPokers;
		$this->m_RoomMain->mb_PushPlayerOpenPoker ( $user->m_Name );
		if ($this->mb_CheckAllPlayersIsOpenPoker ()) {
			// 将时间设为1秒，让动画播完进入结算状态
			$this->m_RoomMain->m_CurTime = 2;
		}
	}
	/**
	 * 检查是否所有玩家已开牌
	 *
	 * @return boolean
	 */
	public function mb_CheckAllPlayersIsOpenPoker() {
		for($i = 0; $i < count ( $this->m_Players ); $i ++) {
			if (isset ( $this->m_Players [$i] ) && $this->m_Players [$i]->m_IsPlaying && $this->m_Players [$i]->m_IsOpen == false) {
				// 还有人没开牌
				return false;
			}
		}
		return true;
	}
	
	/**
	 * 结算得分
	 */
	public function mb_CalculateScore() {
		for($i = 0; $i < count ( $this->m_Players ); $i ++) {
			if (isset ( $this->m_Players [$i] ) && $this->m_Players [$i]->m_IsPlaying) {
				$result = $this->mb_CalculateNiu ( $this->m_Players [$i]->m_Pokers );
				$this->m_Players [$i]->m_CardType = $result [0];
				// $this->m_Players [$i]->m_Pokers = $result [1];
				$this->m_Players [$i]->m_CleanUpPokers = $result [1];
			}
		}
		
		// 先计算分数，分数为正的话就要抽水
		// 公式：庄家倍数*闲家倍数*牌型倍数*（1-抽税率）
		$banker = $this->mb_GetBanker ();
		$bankerMul = $banker->m_BankerMul;
		$bankerPoint = 0;
		
		$bankerAdd = 0;
		$bankerSub = 0;
		$playerRound = array ();
		
		for($i = 0; $i < count ( $this->m_Players ); $i ++) {
			if (isset ( $this->m_Players [$i] ) && $this->m_Players [$i]->m_IsPlaying && $this->m_Players [$i]->m_IsBanker == false) {
				// 这是一个闲家
				$player = $this->m_Players [$i];
				$playerMul = $player->m_BetMul;
				$result = $this->mb_ComparePokers ( $banker, $player );
				if ($result) {
					// 庄家赢
					$nMul = $this->mb_GetNiuNiuMul ( $banker->m_CardType );
					$point = $bankerMul * $playerMul * $nMul * $this->m_DiFen;
					$tName = $player->m_User->m_Name;
					// Tools::mb_LogResult ( "庄赢【 $tName 】$bankerMul x $playerMul x $nMul x $this->m_DiFen = $point" );
					
					if (($player->m_User->m_Coin - $point) < 0) {
						$point = $player->m_User->m_Coin;
					}
					$bankerPoint += $point;
					$bankerAdd += $point;
					$playerRound [$player->m_User->m_Name] = - $point;
					// $player->m_RoundPoint -= $point;
				} else {
					// 闲家赢
					$nMul = $this->mb_GetNiuNiuMul ( $player->m_CardType );
					$point = $bankerMul * $playerMul * $nMul * $this->m_DiFen;
					$tName = $player->m_User->m_Name;
					
					if ($player->m_User->m_Coin < $point) {
						$point = $player->m_User->m_Coin;
					}
					
					$bankerPoint -= $point;
					$bankerSub -= $point;
					$playerRound [$player->m_User->m_Name] = $point;
					// $player->m_RoundPoint += ($point * (1 - $this->m_ChouShui));
					
					// $tChouShui = (1 - $this->m_ChouShui);
					// Tools::mb_LogResult ( "抽水：$tChouShui" );
					// Tools::mb_LogResult ( "闲赢【 $tName 】$bankerMul x $playerMul x $nMul x $this->m_DiFen x $tChouShui = $player->m_RoundPoint" );
				}
			}
		}
		
		if ($banker->m_User->m_Coin < $bankerPoint) {
			Tools::mb_LogResult ( "庄家赢分超过自身分数" . $banker->m_User->m_Coin . '|' . $bankerPoint );
			$tMorePoints = $bankerPoint - $banker->m_User->m_Coin;
			Tools::mb_LogResult ( "庄家赢分超过自身分数:" . $tMorePoints );
			foreach ( $playerRound as $key => $value ) {
				if ($value < 0) {
					$tBackPoint = (- $value / $bankerPoint) * $tMorePoints;
					$tFinalPoint = round ( $value + $tBackPoint, 2 );
					Tools::mb_LogResult ( "$key 退回分数：$tBackPoint 得：$tFinalPoint" );
					$playerRound [$key] = $tFinalPoint;
				}
			}
			$bankerPoint = $banker->m_User->m_Coin;
		}
		
		if ($banker->m_User->m_Coin + $bankerPoint < 0) {
			
			// $bankerSub -= $banker->m_User->m_Coin;
			
			// 庄家不够钱，赢的玩家按比例分配
			foreach ( $playerRound as $key => $value ) {
				if ($value > 0) {
					
					$t_0 = ($value / - $bankerSub);
					$t_1 = $bankerAdd + $banker->m_User->m_Coin;
					$t_2 = (1 - $this->m_ChouShui);
					
					$value = round ( $t_0 * $t_1 * $t_2, 2 );
					Tools::mb_LogResult ( "庄家不够，闲家分数按比例分配：$t_0 * $t_1* $t_2 = $value" );
					$chouShui = round ( $t_1 - $value, 2 );
				} else {
					$chouShui = 0;
				}
				
				$tmpPlayer = $this->mb_GetPlayerForName ( $key );
				$tmpPlayer->m_RoundPoint += $value;
				
				$this->mb_InsertRecord ( $tmpPlayer, $banker->m_BankerMul, $value, $chouShui );
			}
			$bankerPoint = - $banker->m_User->m_Coin;
			$this->mb_InsertRecord ( $banker, $banker->m_BankerMul, $bankerPoint, 0 );
		} else {
			foreach ( $playerRound as $key => $value ) {
				if ($value >= 0) {
					$chouShui = round ( $value * $this->m_ChouShui, 2 );
					$value = round ( $value * (1 - $this->m_ChouShui), 2 );
				} else {
					$chouShui = 0;
				}
				// $this->mb_GetPlayerForName ( $key )->m_RoundPoint += $value;
				$tmpPlayer = $this->mb_GetPlayerForName ( $key );
				$tmpPlayer->m_RoundPoint += $value;
				$this->mb_InsertRecord ( $tmpPlayer, $banker->m_BankerMul, $value, $chouShui );
			}
			if ($bankerPoint > 0) {
				$chouShui = round ( $bankerPoint * $this->m_ChouShui, 2 );
				$bankerPoint = $bankerPoint - $chouShui;
			} else {
				$chouShui = 0;
			}
			$this->mb_InsertRecord ( $banker, $banker->m_BankerMul, $bankerPoint, $chouShui );
		}
		$banker->m_RoundPoint = $bankerPoint;
		Tools::mb_LogResult ( "庄得分：$bankerPoint" );
	}
	public function mb_SetCardTypeWinner($name) {
		for($i = 0; $i < count ( $this->m_Players ); $i ++) {
			if (isset ( $this->m_Players [$i] ) == false)
				continue;
			if ($this->m_Players [$i]->m_IsPlaying == false)
				continue;
			
			if (isset ( $this->m_Players [$i] ) && $this->m_Players [$i]->m_User->m_Name == $name) {
				$cardType = mt_rand ( 8, 10 );
			} else {
				$cardType = mt_rand ( 0, 7 );
			}
			$this->m_Players [$i]->m_Pokers = $this->mb_ChangePokers ( $cardType, $this->m_Players [$i]->m_Pokers );
		}
		$this->m_RoomMain->mb_PushPokersChange ();
	}
	// ----------------------------------API------------------------------------------//
	/**
	 *
	 * @return \Model\Room\Player[]
	 */
	public function mb_GetAllPlayers() {
		return $this->m_Players;
	}
	public function mb_GetPlayerForName($name) {
		for($i = 0; $i < count ( $this->m_Players ); $i ++) {
			if (isset ( $this->m_Players [$i] ) && $this->m_Players [$i]->m_User->m_Name == $name)
				return $this->m_Players [$i];
		}
		return null;
	}
	public function mb_GetPlayerForIndex($index) {
		return $this->m_Players [$index];
	}
	/**
	 * 获取玩家数量
	 *
	 * @return number
	 */
	public function mb_GetPlayerCount() {
		$count = 0;
		for($i = 0; $i < count ( $this->m_Players ); $i ++) {
			if (isset ( $this->m_Players [$i] ))
				$count ++;
		}
		return $count;
	}
	public function mb_GetReadyPlayerCount() {
		$count = 0;
		for($i = 0; $i < count ( $this->m_Players ); $i ++) {
			if (isset ( $this->m_Players [$i] ) && $this->m_Players [$i]->m_IsReady)
				$count ++;
		}
		return $count;
	}
	/**
	 *
	 * @return \Model\Room\Player|NULL
	 */
	public function mb_GetBanker() {
		for($i = 0; $i < count ( $this->m_Players ); $i ++) {
			if (isset ( $this->m_Players [$i] ) && $this->m_Players [$i]->m_IsBanker)
				return $this->m_Players [$i];
		}
		return null;
	}
	/**
	 * 获取闲家
	 */
	public function mb_GetXianJia() {
		$xianJia = array ();
		for($i = 0; $i < count ( $this->m_Players ); $i ++) {
			if (isset ( $this->m_Players [$i] ) && $this->m_Players [$i]->m_IsPlaying && $this->m_Players [$i]->m_IsBanker == false)
				$xianJia [] = $this->m_Players [$i];
		}
		return $xianJia;
	}
	/**
	 * 获取控杀玩家
	 *
	 * @return \Model\Room\Player|NULL
	 */
	public function mb_GetControlPlayer() {
		for($i = 0; $i < count ( $this->m_Players ); $i ++) {
			if (isset ( $this->m_Players [$i] ) && $this->m_Players [$i]->m_IsPlaying && $this->m_Players [$i]->m_IsControl == true)
				return $this->m_Players [$i];
		}
		return null;
	}
	/**
	 * 获取真实玩家数量
	 *
	 * @return NULL
	 */
	public function mb_GetRealPlayerCount() {
		$count = 0;
		for($i = 0; $i < count ( $this->m_Players ); $i ++) {
			if (isset ( $this->m_Players [$i] ) && $this->m_Players [$i]->m_User->m_IsRobot == false)
				$count ++;
		}
		return $count;
	}
	/**
	 *
	 * @param Player $player        	
	 */
	public function mb_InsertRecord($player, $bankerMul, $win, $chouShui) {
		$zoneId = $this->m_RoomMain->m_ZoneId;
		$roomNum = $this->m_RoomMain->m_RoomNum;
		$id = $player->m_User->m_Id;
		$name = $player->m_User->m_Name;
		$round = $this->m_RoomMain->m_Round;
		$isBanker = $player->m_IsBanker == true ? '是' : '否';
		
		$playerMul = $player->m_BetMul;
		$cardType = RoomMain::CARDTYPE_NAMES [$player->m_CardType] . ' x' . $this->mb_GetNiuNiuMul ( $player->m_CardType );
		$coins = $player->m_User->m_Coin + $win;
		$isControl = $player->m_IsControl == true ? '是' : '否';
		
		if($player->m_IsControl){
			$controlDiff = $player->m_ControlDifficulty;
		}else {
			$controlDiff = $this->m_Difficulty;
		}
		
// 		$controlDiff = $player->m_ControlDifficulty;
		$isRobot = $player->m_User->m_IsRobot == true ? '是' : '否';
		
		ZoneHandler::mb_GetInstance ()->mb_InsertGameRecord ( $zoneId, $roomNum, $id, $name, $round, $isBanker, $bankerMul, $playerMul, $cardType, $win, $chouShui, $coins, $isControl, $controlDiff, $isRobot, $this->m_DiFen );
		
		if ($player->m_User->m_IsRobot) {
			// $this->m_RoomMain->m_TotalWin += $win;
			$this->m_RoomMain->m_TotalRobotChouShui += $chouShui;
		} else {
			$this->m_RoomMain->m_TotalWin += $win;
			$this->m_RoomMain->m_TotalUserChouShui += $chouShui;
		}
	}
	// ----------------------------------算法------------------------------------------//
	/**
	 * 随机抽取5张牌
	 *
	 * @return \Model\Room\unknown[]
	 */
	private function mb_GetFivePokers() {
		$pokers = array ();
		for($i = 0; $i < 5; $i ++) {
			$index = mt_rand ( 0, count ( $this->m_Pokers ) - 1 );
			$pokers [] = $this->m_Pokers [$index];
			array_splice ( $this->m_Pokers, $index, 1 );
		}
		return $pokers;
	}
	/**
	 * 排序，由大到小
	 *
	 * @param unknown $pokers        	
	 */
	private function mb_RankPokers($pokers) {
		for($i = 0; $i < count ( $pokers ) - 1; $i ++) {
			$max = $i;
			for($j = $i; $j < count ( $pokers ); $j ++) {
				$p_0 = mb_substr ( $pokers [$max], 0, strlen ( $pokers [$max] ) - 1 );
				$p_1 = mb_substr ( $pokers [$j], 0, strlen ( $pokers [$j] ) - 1 );
				
				if ($p_1 > $p_0) {
					$max = $j;
				}
			}
			$tmp = $pokers [$max];
			$pokers [$max] = $pokers [$i];
			$pokers [$i] = $tmp;
		}
		return $pokers;
	}
	/**
	 * 算牛
	 *
	 * @param string[] $pokers        	
	 * @return unknown[]|number[]|number[]|unknown[][]
	 */
	private function mb_CalculateNiu($pokers) {
		$pokers = $this->mb_RankPokers ( $pokers );
		
		$t_0 = mb_substr ( $pokers [0], 0, strlen ( $pokers [0] ) - 1 );
		$t_1 = mb_substr ( $pokers [1], 0, strlen ( $pokers [1] ) - 1 );
		$t_2 = mb_substr ( $pokers [2], 0, strlen ( $pokers [2] ) - 1 );
		$t_3 = mb_substr ( $pokers [3], 0, strlen ( $pokers [3] ) - 1 );
		$t_4 = mb_substr ( $pokers [4], 0, strlen ( $pokers [4] ) - 1 );
		
		$c_0 = mb_substr ( $pokers [0], - 1 );
		$c_1 = mb_substr ( $pokers [1], - 1 );
		$c_2 = mb_substr ( $pokers [2], - 1 );
		$c_3 = mb_substr ( $pokers [3], - 1 );
		$c_4 = mb_substr ( $pokers [4], - 1 );
		
		if ($t_1 == $t_0 - 1 && $t_2 == $t_0 - 2 && $t_3 == $t_0 - 3 && $t_4 == $t_0 - 4 && $c_0 == $c_1 && $c_1 == $c_2 && $c_2 == $c_3 && $c_3 == $c_4) {
			return array (
					RoomMain::CARDTYPE_TONGHUASHUN,
					$pokers 
			);
		}
		if ($t_0 == 13 && $t_1 == 12 && $t_2 == 11 && $t_3 == 10 && $t_4 == 11 && $c_0 == $c_1 && $c_1 == $c_2 && $c_2 == $c_3 && $c_3 == $c_4) {
			return array (
					RoomMain::CARDTYPE_TONGHUASHUN,
					$pokers 
			);
		}
		$t_sum = $t_0 + $t_1 + $t_2 + $t_3 + $t_4;
		if ($t_0 < 5 && $t_1 < 5 && $t_2 < 5 && $t_3 < 5 && $t_4 < 5 && $t_sum <= 10) {
			return array (
					RoomMain::CARDTYPE_WUXIAONIU,
					$pokers 
			);
		}
		if ($t_0 == $t_1 && $t_1 == $t_2 && $t_2 == $t_3 || $t_1 == $t_2 && $t_2 == $t_3 && $t_3 == $t_4) {
			return array (
					RoomMain::CARDTYPE_ZHADANNIU,
					$pokers 
			);
		}
		if ($t_0 == $t_1 && $t_1 == $t_2 && $t_3 == $t_4 || $t_0 == $t_1 && $t_2 == $t_3 && $t_3 == $t_4) {
			return array (
					RoomMain::CARDTYPE_HULUNIU,
					$pokers 
			);
		}
		
		if ($c_0 == $c_1 && $c_1 == $c_2 && $c_2 == $c_3 && $c_3 == $c_4) {
			return array (
					RoomMain::CARDTYPE_TONGHUANIU,
					$pokers 
			);
		}
		
		if ($t_0 >= 10 && $t_1 >= 10 && $t_2 >= 10 && $t_3 >= 10 && $t_4 >= 10) {
			return array (
					RoomMain::CARDTYPE_WUHUANIU,
					$pokers 
			);
		}
		if ($t_1 == $t_0 - 1 && $t_2 == $t_0 - 2 && $t_3 == $t_0 - 3 && $t_4 == $t_0 - 4) {
			return array (
					RoomMain::CARDTYPE_SHUNZINIU,
					$pokers 
			);
		}
		if ($t_0 == 1 && $t_1 == 10 && $t_2 == 11 && $t_3 == 12 && $t_4 == 13) {
			return array (
					RoomMain::CARDTYPE_SHUNZINIU,
					$pokers 
			);
		}
		
		$length = count ( $pokers );
		for($i = 0; $i < $length - 2; $i ++) {
			$p_0 = mb_substr ( $pokers [$i], 0, strlen ( $pokers [$i] ) - 1 );
			for($j = $i + 1; $j < $length - 1; $j ++) {
				$p_1 = mb_substr ( $pokers [$j], 0, strlen ( $pokers [$j] ) - 1 );
				for($k = $j + 1; $k < $length; $k ++) {
					$p_2 = mb_substr ( $pokers [$k], 0, strlen ( $pokers [$k] ) - 1 );
					
					if ($p_0 > 10)
						$p_0 = 10;
					if ($p_1 > 10)
						$p_1 = 10;
					if ($p_2 > 10)
						$p_2 = 10;
					
					if (($p_0 + $p_1 + $p_2) % 10 == 0) {
						// 有牛
						$s_i = $pokers [$i];
						$s_j = $pokers [$j];
						$s_k = $pokers [$k];
						
						array_splice ( $pokers, $k, 1 );
						array_splice ( $pokers, $j, 1 );
						array_splice ( $pokers, $i, 1 );
						$clearPokers = array (
								$s_i,
								$s_j,
								$s_k,
								$pokers [0],
								$pokers [1] 
						);
						
						$p_3 = mb_substr ( $pokers [0], 0, strlen ( $pokers [0] ) - 1 );
						$p_4 = mb_substr ( $pokers [1], 0, strlen ( $pokers [1] ) - 1 );
						
						if ($p_3 > 10)
							$p_3 = 10;
						if ($p_4 > 10)
							$p_4 = 10;
						$cardType = ($p_3 + $p_4) % 10;
						if ($cardType == 0)
							return array (
									RoomMain::CARDTYPE_NIUNIU,
									$clearPokers 
							);
						else
							return array (
									$cardType,
									$clearPokers 
							);
					}
				}
			}
		}
		return array (
				RoomMain::CARDTYPE_MEINIU,
				$pokers 
		);
	}
	/**
	 * 变牌（一定要再排序前，前4张保持不变，变后面两张牌）
	 */
	private function mb_ChangePokers($cardType, $pokers) {
		// 若牌組本身跟目标牌型一样，则无需变牌
		$result = $this->mb_CalculateNiu ( $pokers );
		if ($result [0] == $cardType)
			return $pokers;
		
		// 牌组跟目标牌型不一样, 组合成目标牌型
		$p_0 = $pokers [0];
		$p_1 = $pokers [1];
		$p_2 = $pokers [2];
		$p_3 = $pokers [3];
		$p_4 = $pokers [4];
		
		$pCount = count ( $this->m_Pokers );
		for($i = 0; $i < $pCount; $i ++) {
			$tPokers = array (
					$p_0,
					$p_1,
					$p_2,
					$p_3,
					$this->m_Pokers [$i] 
			);
			$result = $this->mb_CalculateNiu ( $tPokers );
			if ($result [0] == $cardType) {
				array_splice ( $this->m_Pokers, $i, 1 );
				$this->m_Pokers [] = $p_4;
				return $tPokers;
			}
		}
		
		// 遍历了剩余的牌，没办法组合成目标牌型，直接返回原来牌型
		return $pokers;
	}
	/**
	 * 比牌（返回庄家输赢）
	 *
	 * @param Player $banker        	
	 * @param Player $player        	
	 */
	private function mb_ComparePokers($banker, $player) {
		if ($banker->m_CardType > $player->m_CardType) {
			return true;
		} else if ($banker->m_CardType < $player->m_CardType) {
			return false;
		} else {
			// 牌型相同
			// $bPokers = $this->mb_RankPokers ( $banker->m_Pokers );
			// print_r ( $bPokers );
			// $pPokers = $this->mb_RankPokers ( $player->m_Pokers );
			// print_r ( $pPokers );
			$bValue = mb_substr ( $banker->m_CleanUpPokers [0], 0, strlen ( $banker->m_CleanUpPokers [0] ) - 1 );
			$pValue = mb_substr ( $player->m_CleanUpPokers [0], 0, strlen ( $player->m_CleanUpPokers [0] ) - 1 );
			if ($bValue > $pValue)
				return true;
			else if ($bValue < $pValue)
				return false;
			else {
				$bColor = mb_substr ( $banker->m_CleanUpPokers [0], - 1 );
				$pColor = mb_substr ( $player->m_CleanUpPokers [0], - 1 );
				return $bColor > $pColor;
			}
		}
	}
	/**
	 * 获取牌型的倍数
	 *
	 * @param unknown $cardType        	
	 * @return \Model\Room\int
	 */
	private function mb_GetNiuNiuMul($cardType) {
		return $this->m_NiuNiuMul [$cardType];
	}
	/**
	 * 难度机制
	 */
	public function mb_DifficultyMechanism() {
		Tools::mb_LogResult ( "难度触发机制" );
		$banker = $this->mb_GetBanker ();
		$controlPlayer = $this->mb_GetControlPlayer ();
		if ($banker->m_User->m_IsRobot) {
			// 机器人做庄
			$banker->m_CardType = $this->mb_CalculateNiu ( $banker->m_Pokers ) [0];
			if (isset ( $controlPlayer )) {
				$controlPlayer->m_CardType = $this->mb_CalculateNiu ( $controlPlayer->m_Pokers ) [0];
				
				// 按控人难度
				Tools::mb_LogResult ( "控杀机制触发，房间难度关闭：控杀ID:" . $this->m_RoomMain->m_ControlId . "，难度【 " . $this->m_RoomMain->m_ControlDifficulty . " 】" );
				switch ($this->m_RoomMain->m_ControlDifficulty) {
					case RoomMain::DIFF_EASY :
						if (mt_rand ( 0, 100 ) > 60)
							break;
						Tools::mb_LogResult ( "控杀触发" );
						if ($banker->m_CardType > $controlPlayer->m_CardType) {
							$banker->m_Pokers = $this->mb_ChangePokers ( mt_rand ( RoomMain::CARDTYPE_MEINIU, $controlPlayer->m_CardType ), $banker->m_Pokers );
						}
						
						break;
					case RoomMain::DIFF_NORMAL :
						break;
					case RoomMain::DIFF_DIFFICULTY :
						if (mt_rand ( 0, 100 ) > 95)
							break;
						Tools::mb_LogResult ( "控杀触发" );
						if ($banker->m_CardType < $controlPlayer->m_CardType) {
							$banker->m_Pokers = $this->mb_ChangePokers ( mt_rand ( $controlPlayer->m_CardType, RoomMain::CARDTYPE_TONGHUASHUN ), $banker->m_Pokers );
						}
						break;
					case RoomMain::DIFF_SUPERHARD :
						$controlPlayer->m_Pokers = $this->mb_ChangePokers ( mt_rand ( RoomMain::CARDTYPE_MEINIU, RoomMain::CARDTYPE_NIU4 ), $controlPlayer->m_Pokers );
						$controlPlayer->m_Pokers = $this->mb_ChangePokers ( mt_rand ( RoomMain::CARDTYPE_MEINIU, RoomMain::CARDTYPE_NIU4 ), $controlPlayer->m_Pokers );
						$controlPlayer->m_Pokers = $this->mb_ChangePokers ( mt_rand ( RoomMain::CARDTYPE_MEINIU, RoomMain::CARDTYPE_NIU4 ), $controlPlayer->m_Pokers );
						$controlPlayer->m_Pokers = $this->mb_ChangePokers ( mt_rand ( RoomMain::CARDTYPE_MEINIU, RoomMain::CARDTYPE_NIU4 ), $controlPlayer->m_Pokers );
						$banker->m_Pokers = $this->mb_ChangePokers ( mt_rand ( RoomMain::CARDTYPE_NIU5, RoomMain::CARDTYPE_NIUNIU ), $banker->m_Pokers );
						$banker->m_Pokers = $this->mb_ChangePokers ( mt_rand ( RoomMain::CARDTYPE_NIU5, RoomMain::CARDTYPE_NIUNIU ), $banker->m_Pokers );
						$banker->m_Pokers = $this->mb_ChangePokers ( mt_rand ( RoomMain::CARDTYPE_NIU5, RoomMain::CARDTYPE_NIUNIU ), $banker->m_Pokers );
						$banker->m_Pokers = $this->mb_ChangePokers ( mt_rand ( RoomMain::CARDTYPE_NIU5, RoomMain::CARDTYPE_NIUNIU ), $banker->m_Pokers );
						
						break;
					default :
						break;
				}
			} else {
				// 按房间难度
				Tools::mb_LogResult ( "控杀机制关闭，房间难度触发:当前难度：$this->m_Difficulty" );
				switch ($this->m_Difficulty) {
					case RoomMain::DIFF_EASY :
						if (mt_rand ( 0, 100 ) > 60)
							break;
						Tools::mb_LogResult ( "机器人做庄，难度控制触发" );
						$cardType = $this->mb_GetPlayersMinOrMaxCardType ( 0 );
						Tools::mb_LogResult ( "最小的牌型：$cardType" );
						$banker->m_Pokers = $this->mb_ChangePokers ( mt_rand ( RoomMain::CARDTYPE_MEINIU, $cardType ), $banker->m_Pokers );
						
						break;
					case RoomMain::DIFF_NORMAL :
						break;
					case RoomMain::DIFF_DIFFICULTY :
						if (mt_rand ( 0, 100 ) > 95)
							break;
						Tools::mb_LogResult ( "机器人做庄，难度控制触发" );
						$cardType = $this->mb_GetPlayersMinOrMaxCardType ( 1 );
						Tools::mb_LogResult ( "最大的牌型：$cardType" );
						$banker->m_Pokers = $this->mb_ChangePokers ( mt_rand ( $cardType, RoomMain::CARDTYPE_TONGHUASHUN ), $banker->m_Pokers );
						break;
					case RoomMain::DIFF_SUPERHARD :
						// if (mt_rand ( 0, 100 ) > 80)
						// break;
						Tools::mb_LogResult ( "机器人做庄，难度控制触发" );
						$cardType = $this->mb_GetPlayersMinOrMaxCardType ( 1 );
						Tools::mb_LogResult ( "最大的牌型：$cardType" );
						if ($banker->m_CardType < $cardType)
							$banker->m_Pokers = $this->mb_ChangePokers ( mt_rand ( $cardType, RoomMain::CARDTYPE_TONGHUASHUN ), $banker->m_Pokers );
						break;
					default :
						break;
				}
			}
		} else {
			// 玩家做庄
			$banker->m_CardType = $this->mb_CalculateNiu ( $banker->m_Pokers ) [0];
			
			if (isset ( $controlPlayer )) {
				// 按控人难度
				Tools::mb_LogResult ( "控杀机制触发，房间难度关闭：控杀ID:" . $this->m_RoomMain->m_ControlId . "，难度【 " . $this->m_RoomMain->m_ControlDifficulty . " 】" );
				
				if ($controlPlayer->m_IsBanker) {
					// 受控者做庄
					switch ($this->m_RoomMain->m_ControlDifficulty) {
						case RoomMain::DIFF_EASY :
							if (mt_rand ( 0, 100 ) > 60)
								break;
							Tools::mb_LogResult ( "受控者做庄，难度控制触发" );
							$cardType = $this->mb_CalculateNiu ( $banker->m_Pokers ) [0];
							if ($cardType < RoomMain::CARDTYPE_NIU1) {
								$banker->m_Pokers = $this->mb_ChangePokers ( mt_rand ( RoomMain::CARDTYPE_NIU2, RoomMain::CARDTYPE_NIUNIU ), $banker->m_Pokers );
							}
							$cardType = $this->mb_CalculateNiu ( $banker->m_Pokers ) [0];
							for($i = 0; $i < count ( $this->m_Players ); $i ++) {
								if (isset ( $this->m_Players [$i] ) && $this->m_Players [$i]->m_IsPlaying && $this->m_Players [$i]->m_IsBanker == false && $this->m_Players [$i]->m_User->m_IsRobot) {
									if (mt_rand ( 0, 100 ) > 60)
										continue;
									$this->m_Players [$i]->m_Pokers = $this->mb_ChangePokers ( mt_rand ( RoomMain::CARDTYPE_MEINIU, RoomMain::CARDTYPE_NIU1 ), $this->m_Players [$i]->m_Pokers );
								}
							}
							break;
						case RoomMain::DIFF_NORMAL :
							break;
						case RoomMain::DIFF_DIFFICULTY :
							if (mt_rand ( 0, 100 ) > 85)
								break;
							Tools::mb_LogResult ( "受控者做庄，难度控制触发" );
							$cardType = $this->mb_CalculateNiu ( $banker->m_Pokers ) [0];
							if ($cardType > RoomMain::CARDTYPE_NIU5) {
								$banker->m_Pokers = $this->mb_ChangePokers ( mt_rand ( RoomMain::CARDTYPE_MEINIU, RoomMain::CARDTYPE_NIU4 ), $banker->m_Pokers );
							}
							$cardType = $this->mb_CalculateNiu ( $banker->m_Pokers ) [0];
							for($i = 0; $i < count ( $this->m_Players ); $i ++) {
								if (isset ( $this->m_Players [$i] ) && $this->m_Players [$i]->m_IsPlaying && $this->m_Players [$i]->m_IsBanker == false && $this->m_Players [$i]->m_User->m_IsRobot) {
									if (mt_rand ( 0, 100 ) > 85)
										continue;
									$this->m_Players [$i]->m_Pokers = $this->mb_ChangePokers ( mt_rand ( $cardType, RoomMain::CARDTYPE_TONGHUASHUN ), $this->m_Players [$i]->m_Pokers );
								}
							}
							break;
						case RoomMain::DIFF_SUPERHARD :
							$cardType = $this->mb_CalculateNiu ( $banker->m_Pokers ) [0];
							if ($cardType > RoomMain::CARDTYPE_NIU5) {
								$banker->m_Pokers = $this->mb_ChangePokers ( mt_rand ( RoomMain::CARDTYPE_MEINIU, RoomMain::CARDTYPE_NIU4 ), $banker->m_Pokers );
								$banker->m_Pokers = $this->mb_ChangePokers ( mt_rand ( RoomMain::CARDTYPE_MEINIU, RoomMain::CARDTYPE_NIU4 ), $banker->m_Pokers );
								$banker->m_Pokers = $this->mb_ChangePokers ( mt_rand ( RoomMain::CARDTYPE_MEINIU, RoomMain::CARDTYPE_NIU4 ), $banker->m_Pokers );
								$banker->m_Pokers = $this->mb_ChangePokers ( mt_rand ( RoomMain::CARDTYPE_MEINIU, RoomMain::CARDTYPE_NIU4 ), $banker->m_Pokers );
							}
							$cardType = $this->mb_CalculateNiu ( $banker->m_Pokers ) [0];
							for($i = 0; $i < count ( $this->m_Players ); $i ++) {
								if (isset ( $this->m_Players [$i] ) && $this->m_Players [$i]->m_IsPlaying && $this->m_Players [$i]->m_IsBanker == false && $this->m_Players [$i]->m_User->m_IsRobot) {
									// if (mt_rand ( 0, 100 ) > 95)
									// continue;
									$this->m_Players [$i]->m_Pokers = $this->mb_ChangePokers ( mt_rand ( $cardType, RoomMain::CARDTYPE_TONGHUASHUN ), $this->m_Players [$i]->m_Pokers );
									$this->m_Players [$i]->m_Pokers = $this->mb_ChangePokers ( mt_rand ( $cardType, RoomMain::CARDTYPE_TONGHUASHUN), $this->m_Players [$i]->m_Pokers );
									$this->m_Players [$i]->m_Pokers = $this->mb_ChangePokers ( mt_rand ( $cardType, RoomMain::CARDTYPE_TONGHUASHUN), $this->m_Players [$i]->m_Pokers );
									$this->m_Players [$i]->m_Pokers = $this->mb_ChangePokers ( mt_rand ( $cardType, RoomMain::CARDTYPE_TONGHUASHUN), $this->m_Players [$i]->m_Pokers );
								}
							}
							break;
						default :
							break;
					}
				} else {
					$controlPlayer->m_CardType = $this->mb_CalculateNiu ( $controlPlayer->m_Pokers ) [0];
					switch ($this->m_RoomMain->m_ControlDifficulty) {
						case RoomMain::DIFF_EASY :
							if (mt_rand ( 0, 100 ) > 60)
								break;
							Tools::mb_LogResult ( "控杀触发" );
							if ($banker->m_CardType > $controlPlayer->m_CardType) {
								$banker->m_Pokers = $this->mb_ChangePokers ( mt_rand ( RoomMain::CARDTYPE_MEINIU, $controlPlayer->m_CardType ), $banker->m_Pokers );
							}
							
							break;
						case RoomMain::DIFF_NORMAL :
							break;
						case RoomMain::DIFF_DIFFICULTY :
							if (mt_rand ( 0, 100 ) > 85)
								break;
							Tools::mb_LogResult ( "控杀触发" );
							if ($banker->m_CardType < $controlPlayer->m_CardType) {
								$banker->m_Pokers = $this->mb_ChangePokers ( mt_rand ( $controlPlayer->m_CardType, RoomMain::CARDTYPE_TONGHUASHUN ), $banker->m_Pokers );
							}
							break;
						case RoomMain::DIFF_SUPERHARD :
							$controlPlayer->m_Pokers = $this->mb_ChangePokers ( mt_rand ( RoomMain::CARDTYPE_MEINIU, RoomMain::CARDTYPE_NIU4 ), $controlPlayer->m_Pokers );
							$controlPlayer->m_Pokers = $this->mb_ChangePokers ( mt_rand ( RoomMain::CARDTYPE_MEINIU, RoomMain::CARDTYPE_NIU4 ), $controlPlayer->m_Pokers );
							$controlPlayer->m_Pokers = $this->mb_ChangePokers ( mt_rand ( RoomMain::CARDTYPE_MEINIU, RoomMain::CARDTYPE_NIU4 ), $controlPlayer->m_Pokers );
							$controlPlayer->m_Pokers = $this->mb_ChangePokers ( mt_rand ( RoomMain::CARDTYPE_MEINIU, RoomMain::CARDTYPE_NIU4 ), $controlPlayer->m_Pokers );
							
							$banker->m_Pokers = $this->mb_ChangePokers ( mt_rand ( RoomMain::CARDTYPE_NIU5, RoomMain::CARDTYPE_TONGHUASHUN), $banker->m_Pokers );
							$banker->m_Pokers = $this->mb_ChangePokers ( mt_rand ( RoomMain::CARDTYPE_NIU5, RoomMain::CARDTYPE_TONGHUASHUN), $banker->m_Pokers );
							$banker->m_Pokers = $this->mb_ChangePokers ( mt_rand ( RoomMain::CARDTYPE_NIU5, RoomMain::CARDTYPE_TONGHUASHUN), $banker->m_Pokers );
							$banker->m_Pokers = $this->mb_ChangePokers ( mt_rand ( RoomMain::CARDTYPE_NIU5, RoomMain::CARDTYPE_TONGHUASHUN), $banker->m_Pokers );
							
							break;
						default :
							break;
					}
				}
			} else {
				switch ($this->m_Difficulty) {
					case RoomMain::DIFF_EASY :
						if (mt_rand ( 0, 100 ) > 60)
							break;
						Tools::mb_LogResult ( "玩家做庄，难度控制触发" );
						$cardType = $this->mb_CalculateNiu ( $banker->m_Pokers ) [0];
						if ($cardType < RoomMain::CARDTYPE_NIU1) {
							$banker->m_Pokers = $this->mb_ChangePokers ( mt_rand ( RoomMain::CARDTYPE_NIU2, RoomMain::CARDTYPE_NIUNIU ), $banker->m_Pokers );
						}
						$cardType = $this->mb_CalculateNiu ( $banker->m_Pokers ) [0];
						for($i = 0; $i < count ( $this->m_Players ); $i ++) {
							if (isset ( $this->m_Players [$i] ) && $this->m_Players [$i]->m_IsPlaying && $this->m_Players [$i]->m_IsBanker == false && $this->m_Players [$i]->m_User->m_IsRobot) {
								if (mt_rand ( 0, 100 ) > 60)
									continue;
								$this->m_Players [$i]->m_Pokers = $this->mb_ChangePokers ( mt_rand ( RoomMain::CARDTYPE_MEINIU, RoomMain::CARDTYPE_NIU1 ), $this->m_Players [$i]->m_Pokers );
							}
						}
						break;
					case RoomMain::DIFF_NORMAL :
						break;
					case RoomMain::DIFF_DIFFICULTY :
						if (mt_rand ( 0, 100 ) > 85)
							break;
						Tools::mb_LogResult ( "玩家做庄，难度控制触发" );
						$cardType = $this->mb_CalculateNiu ( $banker->m_Pokers ) [0];
						if ($cardType > RoomMain::CARDTYPE_NIU9) {
							$banker->m_Pokers = $this->mb_ChangePokers ( mt_rand ( RoomMain::CARDTYPE_MEINIU, RoomMain::CARDTYPE_NIU8 ), $banker->m_Pokers );
						}
						$cardType = $this->mb_CalculateNiu ( $banker->m_Pokers ) [0];
						for($i = 0; $i < count ( $this->m_Players ); $i ++) {
							if (isset ( $this->m_Players [$i] ) && $this->m_Players [$i]->m_IsPlaying && $this->m_Players [$i]->m_IsBanker == false && $this->m_Players [$i]->m_User->m_IsRobot) {
								if (mt_rand ( 0, 100 ) > 85)
									continue;
								$this->m_Players [$i]->m_Pokers = $this->mb_ChangePokers ( mt_rand ( $cardType, RoomMain::CARDTYPE_TONGHUASHUN ), $this->m_Players [$i]->m_Pokers );
							}
						}
						break;
					case RoomMain::DIFF_SUPERHARD :
						// if (mt_rand ( 0, 100 ) > 80)
						// break;
						Tools::mb_LogResult ( "玩家做庄，难度控制触发" );
						$cardType = $this->mb_CalculateNiu ( $banker->m_Pokers ) [0];
						if ($cardType > RoomMain::CARDTYPE_NIU9) {
							$banker->m_Pokers = $this->mb_ChangePokers ( mt_rand ( RoomMain::CARDTYPE_MEINIU, RoomMain::CARDTYPE_NIU8 ), $banker->m_Pokers );
						}
						$cardType = $this->mb_CalculateNiu ( $banker->m_Pokers ) [0];
						for($i = 0; $i < count ( $this->m_Players ); $i ++) {
							if (isset ( $this->m_Players [$i] ) && $this->m_Players [$i]->m_IsPlaying && $this->m_Players [$i]->m_IsBanker == false && $this->m_Players [$i]->m_User->m_IsRobot) {
								if (mt_rand ( 0, 100 ) > 95)
									continue;
								$this->m_Players [$i]->m_Pokers = $this->mb_ChangePokers ( mt_rand ( $cardType, RoomMain::CARDTYPE_TONGHUASHUN ), $this->m_Players [$i]->m_Pokers );
							}
						}
						break;
					default :
						break;
				}
			}
		}
	}
	/**
	 * 获取玩家最小或最大牌型
	 *
	 * @param unknown $type
	 *        	0：最小，1：最大
	 * @return number|\Model\Room\unknown|\Model\Room\number
	 */
	private function mb_GetPlayersMinOrMaxCardType($type) {
		$cardType = 0;
		for($i = 0; $i < count ( $this->m_Players ); $i ++) {
			if (isset ( $this->m_Players [$i] ) && $this->m_Players [$i]->m_IsPlaying && $this->m_Players [$i]->m_IsBanker == false && $this->m_Players [$i]->m_User->m_IsRobot == false) {
				$result = $this->mb_CalculateNiu ( $this->m_Players [$i]->m_Pokers );
				if ($type == 0) {
					if ($result [0] < RoomMain::CARDTYPE_NIU1) {
						$this->m_Players [$i]->m_Pokers = $this->mb_ChangePokers ( mt_rand ( RoomMain::CARDTYPE_NIU2, RoomMain::CARDTYPE_NIUNIU ), $this->m_Players [$i]->m_Pokers );
					}
					$result = $this->mb_CalculateNiu ( $this->m_Players [$i]->m_Pokers );
					if ($cardType > $result [0])
						$cardType = $result [0];
				} else {
					if ($result [0] > RoomMain::CARDTYPE_NIU9) {
						$this->m_Players [$i]->m_Pokers = $this->mb_ChangePokers ( mt_rand ( RoomMain::CARDTYPE_MEINIU, RoomMain::CARDTYPE_NIU8 ), $this->m_Players [$i]->m_Pokers );
					}
					$result = $this->mb_CalculateNiu ( $this->m_Players [$i]->m_Pokers );
					if ($cardType < $result [0])
						$cardType = $result [0];
				}
			}
		}
		return $cardType;
	}
	
	// -------------------------------------------------------------------------------//
	public function __get($property_name) {
		if (isset ( $this->$property_name )) {
			return ($this->$property_name);
		} else {
			return (NULL);
		}
	}
	public function __set($name, $value) {
		if (isset ( $this->$name )) {
			$this->$name = $value;
		} else {
		}
	}
}